
#import "PublicBottmMenuView.h"

#define  MENU_CEEL_HEIGHT 30

@implementation PublicBottmMenuView
{
    
    int buttonNumber;
    int buttonWidth;
    
}


@synthesize detailArr;
@synthesize delegate;
@synthesize popupTableView;
@synthesize tableData;
@synthesize nCurselectIndex;


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        popupTableView = [[UITableView alloc]initWithFrame:CGRectZero];
        popupTableView.delegate = self;
        popupTableView.dataSource = self;
        popupTableView.bounces = NO;
        
        popupTableView.backgroundColor =[UIColor whiteColor];
        [self addSubview:popupTableView];
    }
    self.backgroundColor = [UIColor whiteColor];
    _ispulldown = NO;
    nCurselectIndex = -1;
    return self;
}

-(void) getBottonMenuData:(NSString*)appId{

    NSDictionary * lastData = [DE serverCall:@"queryMenus" params:@{@"public_id":appId} callback:^(BOOL success, int code, NSString *desc, NSDictionary *data, NSDictionary *params) {
        if (success){
            NSLog(@"---------release?%@",delegate);
             if (delegate) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    detailArr = [[data objectForKey:@"menu"] objectForKey:@"button"];
                    [delegate onPublicBottomMenuInitComplete:data];
                    [self initMenu];
                
                });
            }
        }
        
    } needCache:YES];
    
    if (lastData) {
        if (delegate) {
            detailArr = [[lastData objectForKey:@"menu"] objectForKey:@"button"];
            [delegate onPublicBottomMenuInitComplete:lastData];
            [self initMenu];
        }
    }
}

-(void) initMenu{
    
    
    if (!detailArr) {
        return;
    }
    
    buttonNumber = (int)detailArr.count;
    buttonWidth = self.frame.size.width/buttonNumber;
    
    for (int a= 0; a < detailArr.count; a++) {
        
        NSDictionary* dataMp = [detailArr objectAtIndex:a];
        
        UIButton * menuBtn = [[UIButton alloc] initWithFrame:CGRectMake(buttonWidth*a, 0, buttonWidth, CHART_COMM_HEIGHT)];
        menuBtn.tag = a;
        [menuBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [menuBtn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
        menuBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14.0f];

        [menuBtn addTarget:self action:@selector(selectBtn:) forControlEvents:UIControlEventTouchUpInside];
        [menuBtn setTitle:[dataMp objectForKey:@"name"] forState:UIControlStateNormal];
        [self addSubview:menuBtn];
    }
 
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return MENU_CEEL_HEIGHT;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //NSLog(@"height =%i" ,tableData.count);
    return tableData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString * CellIdentifier = @"Cell";
    UITableViewCell * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.textLabel.font = [UIFont boldSystemFontOfSize:14.0f];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        
    }
    cell.textLabel.text = [[tableData objectAtIndex:indexPath.row] objectForKey:@"name"];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (delegate) {
        [delegate onPublicMenuItemClick:[tableData objectAtIndex:indexPath.row]];
    }
    
    popupTableView.hidden = YES;
}

-(void)selectBtn:(UIButton*)btn
{
    NSLog(@"点击每个小项目");
    NSInteger index = btn.tag;
    tableData = [[detailArr objectAtIndex:index] objectForKey:@"sub_button"];
   // NSLog(@"tableData count=%@",tableData);
    int heigth = -tableData.count *MENU_CEEL_HEIGHT;
    if (tableData && tableData.count>0) {
        //点击的是之前的索引
        if (nCurselectIndex == index && !popupTableView.hidden) {
            popupTableView.hidden = YES;
            return;
        }
        popupTableView.frame = CGRectMake(buttonWidth*index-index*20, heigth, buttonWidth+(detailArr.count >2?40:0),MENU_CEEL_HEIGHT *tableData.count);
        [popupTableView reloadData];
        popupTableView.hidden = NO;
        nCurselectIndex = index;
    }
    else{
        if (delegate) {
            [delegate onPublicMenuItemClick:[detailArr objectAtIndex:index]];
        }
    }
    
}

#pragma mark - 超出fram部分的点击事件处理

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    
    CGPoint hitPoint = [popupTableView convertPoint:point fromView:self];
    if ([popupTableView pointInside:hitPoint withEvent:event])
        return popupTableView;
    return [super hitTest:point withEvent:event];
}



@end
